package com.codejiwei.core.demo

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object WordCount2 {
  def main(args: Array[String]): Unit = {

    val conf: SparkConf = new SparkConf()
      .setMaster("local")
      .setAppName("WordCount")

    val sc = new SparkContext(conf)

    val lines: RDD[String] = sc.textFile("E:\\workspace\\spark-app-sdk\\spark-core\\src\\main\\resources\\word.txt")

    //方式1
    val result = lines.flatMap(x => x.split(" "))
      .groupBy(word => word)
      .map{
        case (x, y) => (x, y.size)
      }
      .sortBy(_._2, false)
      .collect()

    //方式2
    val result2 = lines.flatMap(line => line.split(" "))
      .map {
        case word => (word, 1)
      }
      .reduceByKey((x, y) => x + y)
//      .sortByKey()
      .sortBy {
        case (word, count) => -count
      }
      .take(3)
//      .collect()


    result2.foreach(println)

    sc.stop()
  }
}
